<template>
  <!-- <Header></Header> -->
  <div class="page-container">
    <div class="banner" ref="banner">
      <h1>今时今日，恰如其分</h1>
      <Typing class="typing-container"></Typing>
    </div>
    <!-- 新增一个与banner高度相同的占位元素，使内容从banner下方开始 -->
    <div class="banner-placeholder"></div>
    <!-- 内容区容器 -->
    <div class="content-container">
      <!-- 滚动图标 -->
      <DownOutlined @click="scrollToContent" class="down-icon"/>
      <!-- 波浪效果 -->
      <Wave class="wave-about"></Wave>
      <!-- 内容区 -->
      <a-spin tip="加载中..." :spinning="spinning">
        <div class="content-board" ref="contentRef">
          <div class="content-data">
            <div class="left-content">
              <!-- 个人信息区 -->
              <div class="personal-info">
                <!-- 个人头像 -->
                <div class="personal-avatar">
                  <img src="@/assets/img/avatar.png" alt="">
                </div>
                <div class="personal-username">zhiheng.lu</div>
                <div class="personal-statistics">
                  <div class="item-statistics">
                    <span>文章</span>
                    <span>{{ siteInfo.articleCount }}</span>
                  </div>
                  <div class="item-statistics">
                    <span>分类</span>
                    <span>{{ siteInfo.categoryCount }}</span>
                  </div>
                  <div class="item-statistics">
                    <span>访问量</span>
                    <span>{{ siteInfo.viewCount }}</span>
                  </div>
                </div>
                <div class="wechat-moments" @click="toFriendCycleView()">
                  <StarOutlined/>
                  朋友圈
                </div>
              </div>

              <!-- 搜索区 -->
              <div class="search-container">
                <span>搜索</span>
                <div class="search">
                  <input v-model="searchForm.pageModel.query.title0" class="search-input" @keyup.enter="searchArticle"
                         placeholder="搜索文章"></input>
                  <span class="search-btn iconfont icon-sousuo" @click="searchArticle"></span>
                </div>
              </div>

              <!-- 推荐文章区 -->
              <div class="recommend-article-container">
                <div class="title icon-sm">
                  <svg class="icon" aria-hidden="true" width="20" height="20">
                    <use xlink:href="#icon-schedule_2"></use>
                  </svg>
                  <span>&nbsp;推荐文章</span>
                </div>
                <div class="recommend-article-list">
                  <div v-if="recommendArticleList.length > 0" v-for="(item, index) in recommendArticleList" :key="index"
                       class="article-info" @click="goDetail(item)">
                    <div class="detail">
                      <img :src="item.thumbnail">
                      <span style="">{{ item.title }}</span>
                    </div>
                    <span class="date icon-ssm">
                      <svg class="icon" aria-hidden="true" fill="rgb(163, 163, 163)">
                        <use xlink:href="#icon-rili"></use>
                      </svg>
                      <span>&nbsp;{{ item.createTime }}</span>
                    </span>
                  </div>
                  <div v-else class="tip">暂无推荐文章</div>
                </div>
              </div>

              <!-- 树洞 -->
              <div class="tree-hole-container">
                <div class="title icon-sm">
                  <svg class="icon" aria-hidden="true" fill="rgb(163, 163, 163)">
                    <use xlink:href="#icon-zuocecaidan-songshu"></use>
                  </svg>
                  <span>&nbsp;树洞留言</span>
                </div>
                <CommentScroll :commentList="treeHoleList"></CommentScroll>

              </div>
            </div>

            <div class="right-content">
              <!-- 公告栏 -->
              <div class="notice-board">
                <span class="iconfont icon-tongzhi"></span>&nbsp;&nbsp;
                <span>
                  <a class="my-gitee-address" target="_blank"
                     href="https://gitee.com/lu-zhihen">
                    Gitee：https://gitee.com/lu-zhihen
                  </a>
                </span>
              </div>
              <!-- 内容详情 -->
              <div v-if="!clickSearchBtn" class="content-detail" v-for="(item, index) in categoryArticleList"
                   :key="index">
                <div class="content-detail-title">
                  <div>
                    <span class="iconfont icon-fenlei notice-icon" style="color: #ff623e;"></span>
                    <span>&nbsp;{{ item.categoryName }}</span>
                  </div>
                  <div @click="goMoreArticle(item.categoryId)">
                    <span class="iconfont icon-icon-test notice-icon" style="color: #009f72;"></span>
                    <span>&nbsp;MORE</span>
                  </div>
                </div>
                <div class="content-detail-card">
                  <CommonCard :cardData="card" @click="goDetail(card)" v-for="(card, idx) in item.articleInfoList"
                              :key="card.id"></CommonCard>
                </div>
              </div>
              <div v-else class="search-content-list">
                <CommonCardSearch style="margin-bottom: 40px;" :idx="index" :cardData="card" @click="goDetail(card)"
                                  v-for="(card, index) in searchArticleList" :key="card.id"></CommonCardSearch>
                <div v-if="isSearchLastPage" class="no-next-page">到底啦~~</div>
                <div v-else style="text-align: center;">
                  <button class="next-page-btn" @click="handleNextPage">下一页</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </a-spin>

    </div>

    <div class="blank-info"></div>

    <!-- 播放器 -->
    <MusicPlayer></MusicPlayer>
  </div>
</template>

<script setup>
import {findGuestbookList} from '@/api/guestbook';
import {findAllCategoryArticleList, findArticlePage, findRecommendArticle} from "@/api/article.js";
import {findSystemValueByTypes} from "@/api/system.js";
import CommentScroll from '@/components/CommentScroll.vue';
import CommonCard from '@/components/CommonCard.vue';
import MusicPlayer from "@/components/MusicPlayer.vue";
import Typing from '@/components/Typing.vue';
import Wave from '@/components/Wave.vue';
import {DownOutlined, StarOutlined} from '@ant-design/icons-vue';
import {onMounted, reactive, ref} from 'vue';
import {useRouter} from 'vue-router';
import CommonCardSearch from '@/components/CommonCardSearch.vue';


const spinning = ref(false);
const router = useRouter();
const banner = ref(null);
const contentRef = ref(null);
// 不同分类下的文章列表
const categoryArticleList = ref([]);
// 推荐文章列表
const recommendArticleList = ref([])
// 树洞留言列表
const treeHoleList = ref([]);
const siteForm = ref([
  'total_article_view_count',
  'total_category_count',
  'total_article_count',
])
// 网站信息
const siteInfo = reactive({
  viewCount: 0,
  articleCount: 0,
  categoryCount: 0,
});
// 搜索按钮点击状态
const clickSearchBtn = ref(false);
// 是否搜索最后一页
const isSearchLastPage = ref(false);
// 搜索表单
const searchForm = reactive({
  pageModel: {
    query: {
      title0: '',
      status1: 1,
    },
    pageNow: 1,
    pageSize: 10,
    sort: ['1create_time'],
  }
});
// 搜索文章列表
const searchArticleList = ref([]);
/* 滚动到内容区 */
const scrollToContent = () => {
  if (!contentRef.value) return;

  const targetTop = contentRef.value.getBoundingClientRect().top + window.scrollY;
  const duration = 1000;
  const startTime = performance.now();

  const animateScroll = (currentTime) => {
    const progress = Math.min((currentTime - startTime) / duration, 1);
    window.scrollTo(0, window.scrollY + (targetTop - window.scrollY) * progress * (2 - progress));
    if (progress < 1) requestAnimationFrame(animateScroll);
  };
  requestAnimationFrame(animateScroll);
};

const toFriendCycleView = () => {
  router.push({
    name: 'friendCycle',
  });
};
// 获取树洞弹幕
const getTreeHoleGuestBook = async () => {
  await findGuestbookList({moduleId: 1}).then(res => {
    if (res.code === 200) {
      treeHoleList.value = res.data;
    }
  }).catch(err => {
  })
}
// 跳转文章详情
const goDetail = (item) => {
  router.push({
    name: 'article',
    query: {
      id: item.id,
    },
  });
};
// 跳转更多文章
const goMoreArticle = (categoryId) => {
  router.push({
    name: 'moreArticle',
    query: {
      categoryId: categoryId,
    },
  });
};
// 获取文章列表
const getArticleList = () => {
  findAllCategoryArticleList().then(res => {
    if (res.code === 200) {
      console.log(res.data)
      categoryArticleList.value = res.data;
    }
  }).catch(err => {
  })
}
// 获取推荐文章
const getRecommendArticleList = () => {
  findRecommendArticle().then(res => {
    if (res.code === 200) {
      recommendArticleList.value = res.data;
    }
  }).catch(err => {
  })
}
// 搜索文章
const searchArticle = () => {
  findArticlePage(searchForm).then(res => {
    if (res.code === 200) {
      searchForm.pageModel.pageNow++;
      clickSearchBtn.value = true;
      if (res.data.list.total <= searchArticleList.value.length) {
        // 最后一页
        isSearchLastPage.value = true;
      } else {
        searchArticleList.value = res.data.list.dataList;
        if (res.data.list.total <= searchArticleList.value.length) {
          // 最后一页
          isSearchLastPage.value = true;
        }
      }
    }
  }).catch(err => {
    console.log(err)
  })
}
// 下一页
const handleNextPage = () => {
  findArticlePage(searchForm).then(res => {
    if (res.code === 200) {
      searchForm.pageModel.pageNow++;
      if (res.data.list.total <= searchArticleList.value.length) {
        // 最后一页
        isSearchLastPage.value = true;
      } else {
        searchArticleList.value = [...searchArticleList.value, ...res.data.list.dataList];
        if (res.data.list.total <= searchArticleList.value.length) {
          // 最后一页
          isSearchLastPage.value = true;
        }
      }
    }
  }).catch(err => {
    console.log(err)
  })
}
// 获取网站信息
const getSiteInfo = () => {
  findSystemValueByTypes({typeList: siteForm.value}).then(res => {
    if (res.code === 200) {
      siteInfo.articleCount = res.data['total_article_count'];
      siteInfo.categoryCount = res.data['total_category_count'];
      siteInfo.viewCount = res.data['total_article_view_count'];
    }
  }).catch(err => {
    console.log(err)
  })
}

onMounted(() => {
  spinning.value = true;
  getSiteInfo();
  getTreeHoleGuestBook();
  getArticleList();
  getRecommendArticleList();
  spinning.value = false;
})
</script>

<style scoped lang="less">
.typing-container {
  position: absolute;
  top: 280px;
}

.blank-info {
  margin-top: 20px;
}

.wave-about {
  position: absolute;
  top: -149px;
}

.page-container {
  overflow-x: hidden;
}

.down-icon {
  position: absolute;
  left: 50%;
  top: -75px;
  z-index: 300;
  color: #fff;
  cursor: pointer;
  font-size: 40px;
  animation: move-cicle 0.8s infinite alternate ease-out;
}

/* down icon*/
@keyframes move-cicle {
  0% {
    transform: translateY(-10px)
  }

  100% {
    transform: translateY(10px);
    opacity: 0.5;
  }
}

/* 通知图标缩放动画 */
@keyframes notice-icon-scale {
  0% {
    transform: scale(1);
  }

  100% {
    transform: scale(1.2);
  }
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }

  50% {
    background-position: 100% 50%;
  }

  100% {
    background-position: 0% 50%;
  }
}

.banner {
  position: fixed;
  width: 100%;
  height: var(--top-img-height);
  background: url('@/assets/img/home-bg.jpg') no-repeat center center / cover;
  background-color: rgba(0, 0, 0, 0.1);
  background-blend-mode: overlay;
  top: 0px;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: height 0.3s ease, opacity 0.3s ease;
}

/* 新增的占位元素，高度与banner相同 */
.banner-placeholder {
  height: var(--top-img-height);
}

.banner h1 {
  font-size: 40px;
  color: #fff;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  z-index: 2;
  margin: 0;
  transition: transform 0.3s ease;
  transform-origin: center center;
}

.content-container {
  position: relative;
  z-index: 300;
}

.content-board {
  background-color: #fff;

  .content-data {
    max-width: 1300px;
    margin: 0 auto;
    padding: 5px;
    display: flex;
    justify-content: center;


    .left-content {
      width: 24%;
      margin-right: 20px;
      margin-top: 10px;

      /* 个人信息区 */

      .personal-info {
        height: 345px;

        background: rgba(255, 255, 255, 0.15);
        background: linear-gradient(-45deg, #7678e7, #9279e4, #adf5b8, #e8bc72);
        background-size: 400% 400%;
        animation: gradient 15s ease infinite;

        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-radius: 15px;
        transition: all 0.3s linear;
        box-shadow: var(--card-shadow-1);

        &:hover {
          box-shadow: var(--card-shadow-5);
        }

        .personal-avatar {
          width: 120px;
          height: 120px;

          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: all 0.3s linear;
            border-radius: 50%;

            &:hover {
              transform: rotate(360deg);
              cursor: pointer;
            }
          }
        }

        .personal-username {
          font-size: 30px;
          font-weight: bold;
          margin: 20px 0;
        }

        .personal-statistics {
          display: flex;
          justify-content: center;

          .item-statistics {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            &:nth-child(2) {
              margin: 0 35px;
            }
          }
        }

        .wechat-moments {
          color: #fff;
          font-weight: bold;
          margin-top: 15px;
          padding: 10px 20px;
          border-radius: 50px;
          width: 60%;
          text-align: center;
          cursor: pointer;
          transition: all 0.5s ease-in-out;

          /* 使用线性渐变创建双色背景 */
          background: linear-gradient(to right, #ff4935 50%, var(--text-color2) 50%);
          background-size: 200% 100%;
          background-position: right bottom;

          /* 添加阴影效果 */
          box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);


          &:hover {
            background-position: left bottom;
          }
        }
      }

      /* 搜索区 */

      .search-container {
        margin-top: 20px;
        overflow: hidden;
        box-shadow: var(--card-shadow-1);
        border-radius: 15px;
        padding: 15px;
        transition: all 0.3s linear;

        span {
          font-size: 20px;
          font-weight: bold;
          color: var(--text-color2);
        }

        &:hover {
          box-shadow: var(--card-shadow-5);
        }

        .search {
          display: flex;
          align-items: center;
          justify-content: center;
          position: relative;
          margin-top: 10px;

          .search-input {
            width: 100%;
            height: 35px;
            border: 2px solid var(--text-color2) !important;
            border-radius: 30px;

            flex: 1;
            padding: 8px 12px;
            outline: none;
            font-size: 14px;
          }

          .search-btn {
            color: var(--text-color2);
            position: absolute;
            right: 10px;
            cursor: pointer;
          }
        }
      }

      /* 推荐文章区 */

      .recommend-article-container {
        border-radius: 15px;
        margin-top: 20px;
        box-shadow: var(--card-shadow-1);
        padding: 20px;
        transition: all 0.3s linear;

        &:hover {
          box-shadow: var(--card-shadow-5);
        }

        .title {
          display: flex;
          align-items: center;
          font-weight: bold;
        }

        .recommend-article-list {
          .article-info {
            cursor: pointer;

            .detail {
              display: flex;
              align-items: top;
              width: 100%;
              margin-top: 20px;

              img {
                border-radius: 10px;
                width: 30%;
                object-fit: cover;
                margin-right: 8px;
                /* 强制正方形比例 */
                aspect-ratio: 1/1;

                @media (max-width: 768px) {
                  width: 40%;
                }
              }

              span {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                text-overflow: ellipsis;

                @media (min-width: 768px) {
                  -webkit-line-clamp: 2;
                  /* 根据实际情况调整行高 */
                  line-height: 1.5;
                  /* 确保最大高度不超过2行 */
                  max-height: 3em;
                }

                @media (max-width: 768px) {
                  -webkit-line-clamp: 5;
                  /* 根据实际情况调整行高 */
                  line-height: 1.5;
                  /* 确保最大高度不超过2行 */
                  max-height: 7.5em;
                }

              }
            }

            .date {
              color: var(--text-color3);
              display: flex;
              align-items: center;
              margin-top: 5px;
            }
          }

          .tip {
            color: var(--text-color3);
            text-align: center;
            padding: 20px 0;
          }
        }
      }

      /* 树洞区 */

      .tree-hole-container {
        margin-top: 20px;
        border-radius: 15px;
        box-shadow: var(--card-shadow-1);
        padding: 20px;
        transition: all 0.3s linear;

        &:hover {
          box-shadow: var(--card-shadow-5);
        }


        .title {
          font-weight: bold;
          display: flex;
          align-items: center;
          margin-bottom: 10px;
        }
      }
    }

    .right-content {
      width: 76%;
      padding: 0 0px 20px 20px;
      margin-top: 10px;

      @media (max-width: 768px) {
        width: 100%;
        padding: 0;
      }


      .notice-board {
        width: 100%;
        padding: 20px;
        border-radius: 10px;
        border: 1px dashed var(--dashed-color);
        margin-bottom: 30px;
        color: var(--text-color1);
        font-size: 18px;
        display: flex;
        align-items: center;

        span:first-child {
          font-size: 25px;
          color: #ffa500;
          animation: notice-icon-scale 0.5s infinite alternate ease-out;
        }

        @media (max-width: 768px) {
          margin-top: 20px;
        }

        .my-gitee-address {
          color: var(--text-color1);
        }
      }

      .content-detail {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 10px;
        flex-direction: column;

        .content-detail-title {
          width: 100%;
          color: var(--text-color1);
          border-bottom: 1px dashed var(--dashed-color);
          padding-bottom: 10px;
          margin-bottom: 30px;
          display: flex;
          justify-content: space-between;
          align-items: center;

          .notice-icon {
            font-size: 20px;
            font-weight: bold;
          }

          div {
            display: flex;
            align-items: center;
          }

          div:last-child {
            cursor: pointer;
            transition: all 0.3s linear;

            &:hover {
              transform: scale(1.1);
              font-weight: bold;
              color: #39c5bb;
            }
          }
        }

        .content-detail-card {
          width: 100%;
          display: grid;
          gap: 1.5rem;
          grid-template-columns: repeat(3, minmax(0, 1fr));
          padding: 0 10px;

          @media screen and (max-width: 1400px) {
            grid-template-columns: repeat(2, minmax(0, 1fr));
          }

          @media screen and (max-width: 600px) {
            grid-template-columns: repeat(1, minmax(0, 1fr));
          }
        }
      }
    }

    @media (max-width: 768px) {
      flex-direction: column;
      align-items: center;
      justify-content: center;

      .left-content {
        width: 100%;
        margin: 0 auto;
      }
    }
  }

}

.section {
  margin: 15px 0;
  background-color: rgb(226, 218, 218);
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.no-next-page {
  color: #666;
  text-align: center;
}

.next-page-btn {
  padding: 12px 18px;
  border-radius: 30px;
  color: var(--text-color3);
  border: 1px solid var(--text-color3);
  background-color: #fff;
  cursor: pointer;

  &:hover {
    border: 1px solid #ffb01f;
    color: #ffb01f;
    box-shadow: 0px 0px 10px #ffb01f;
  }
}
</style>